# Makes a stacked line graph by date for aggregate user statistics about work

args <- commandArgs(TRUE)

inputfile <- args[1]
outputfile <- args[2]
worktype <- args[3]	# ["edits", "bytes", "hours"]
datatype <- args[4]	# ["prop", "count"]


data <- read.table(file=inputfile, sep="\t", quote="", comment.char="", header=TRUE)

data$Date <- as.Date(as.character(data$Date), "%Y")

stats <- data[ , 2:ncol(data)]

clusters <- ncol(stats)

xaxis <- c(data$Date, rev(data$Date))

ylim <- c(0, sum(stats))

# ylim <- c(0, max(data$Article + data$Article.Talk + data$User + data$User.Talk + data$Other + data$Maintenance))

polygons <- {}

polygons[[1]] <- c(rep(0, nrow(stats)), rev(stats[ , 1]))

bottom <- 0
top <- rev(stats[ , 1])

for (i in  2:clusters) {

	bottom <- bottom + stats[ , i - 1]
	top <- top + rev(stats[ , i])

	polygons[[length(polygons) + 1]] <- c(bottom, top)

}

# article <- c(rep(0, nrow(data)), rev(data$Article))
# articletalk <- c(data$Article, rev(data$Article) + rev(data$Article.Talk))
# user <- c(data$Article + data$Article.Talk, rev(data$Article) + rev(data$Article.Talk) + rev(data$User))
# usertalk <- c(data$Article + data$Article.Talk + data$User, rev(data$Article) + rev(data$Article.Talk) + rev(data$User) + rev(data$User.Talk))
# other <- c(data$Article + data$Article.Talk + data$User + data$User.Talk, rev(data$Article) + rev(data$Article.Talk) + rev(data$User) + rev(data$User.Talk) + rev(data$Other))
# maintenance <- c(data$Article + data$Article.Talk + data$User + data$User.Talk + data$Other, rev(data$Article) + rev(data$Article.Talk) + rev(data$User) + rev(data$User.Talk) + rev(data$Other) + rev(data$Maintenance))

if (datatype == "prop") {
	dt <- "Proportion Of"
} else if (datatype == "count") {
	dt <- "Total"
} else {
	stop("Invalid data type argument")
}

if (worktype == "edits") {
	wt <- "Edits"
} else if (worktype == "bytes") {
	wt <- "Bytes"
} else if (worktype == "hours") {
	wt <- "Hours"
} else {
	stop("Invalid work type argument")
}

title <- paste(dt, "User-years By Cluster", paste("(", wt, ")", sep=""))
xlabel <- "Start Year Of User-year"
ylabel <- paste(dt, "User-years", paste("(", wt, ")", sep=""))

png(outputfile,width=1000,height=1000)

fontsize=2.0

colors <- rainbow(clusters)

plot(x=data$Date, y=stats[ , 1], ylim=ylim, col=colors[[1]], type='l', xaxt='n', ylab=ylabel, xlab=xlabel, main=title, cex.lab=fontsize, cex.axis=fontsize, cex.main=fontsize, cex.sub=fontsize) 

# plot(x=data$Date, y=data$Article, ylim=ylim, col='lightgreen', type='l', xaxt='n', ylab=ylabel, xlab = xlabel, main=title,cex.lab=fontsize, cex.axis=fontsize, cex.main=fontsize, cex.sub=fontsize)

for (i in 1:clusters) {

	polygon(xaxis, polygons[[i]], col=colors[[i]], border=NA)

}

# polygon(xaxis, article, col='lightgreen', border=NA)
# polygon(xaxis, articletalk, col='darkgreen', border=NA)
# polygon(xaxis, user, col='lightblue', border=NA)
# polygon(xaxis, usertalk, col='darkblue', border=NA)
# polygon(xaxis, other, col='yellow', border=NA)
# polygon(xaxis, maintenance, col='red', border=NA)

labdates <- as.Date('1970-01-01') + min(data$Date):max(data$Date)
labdates <- labdates[ format(labdates, '%d %b') == '01 Jan']
labnames <- format(labdates, '%Y')

axis(1, at=labdates, labels=labnames, cex.axis=fontsize)

dev.off()
